package com.nuist.springbootbook.controller;


import com.nuist.springbootbook.model.UserInfo;
import com.nuist.springbootbook.service.UserService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/user")
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    public String login(String userName, String password, HttpSession session) {

        //  判断用户名和用户密码是否为空
        if (!StringUtils.hasLength(userName) && !StringUtils.hasLength(password)) {
            return "用户名或者密码为空";
        }
        // 调用Service中的方法, Service 中的方法来自于Mapper
        UserInfo userInfo = userService.getUserInfoByName(userName);
        // 如果存在用户,就需要校验密码是否正确
        if (userInfo == null) {
            return "用户不存在";
        }
        if (userName.equals(userInfo.getPassword()) && password.equals(userInfo.getPassword())) {
            session.setAttribute("username", userName);
            return "登录成功";
        }
        return "账号密码错误";
    }
}
